package wafteam.comicview;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import wafteam.database.Chap;
import wafteam.database.Database;
import wafteam.others.ComicArrayAdapter;
import wafteam.transData.TransData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class ChapterListActivity extends Activity {
	
	private ListView chapterListView;
	private Database db;
	private ArrayList<Chap> arrChapters;
	ArrayList<HashMap<String, String>> array;
	private ComicArrayAdapter adapter;
	final Context context = this;
	public static String chapImgLocation = Environment.getExternalStorageDirectory()+"/Comic/";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapter_list);
		
		// Make folder in sdcard0 to store data
		File folder = new File(chapImgLocation);
		if (!folder.exists()){
			folder.mkdirs();
		}
		
		chapterListView = (ListView) findViewById(R.id.chapterList);

		// Connect db, get data and show it on list view
		db = new Database(ChapterListActivity.this); 
		arrChapters = db.getAllChaps();
		db.close();
		adapter = new ComicArrayAdapter(ChapterListActivity.this, R.layout.chapter, arrChapters);
		chapterListView.setAdapter(adapter);
				
		// Processing when a chapter is selected
		chapterListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			
			@Override
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id){
				if (arrChapters.get(position).getIsDownload()==1){
					// If pictures was downloaded
					Intent imgs = new Intent(ChapterListActivity.this, ViewImageActivity.class);
					imgs.putExtra("chapterID", arrChapters.get(position).getChapId());
					startActivity(imgs);
				} else {
					// If pictures was not downloaded yet
					TransData trans = new TransData();
					String data = trans.fetchJSON();
					
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
							context);
					alertDialogBuilder.setTitle(data);
					alertDialogBuilder
					.setMessage(data)
					.setCancelable(false)
					.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							// if this button is clicked, close
							// current activity
							//MainActivity.this.finish();
							dialog.cancel();
						}
					  })
					.setNegativeButton("No",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							// if this button is clicked, just close
							// the dialog box and do nothing
							dialog.cancel();
						}
					});
	 
					// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();
	 
					// show it
					alertDialog.show();
				}		
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chapter_list, menu);
		return true;
	}
	public void abc(){
		Toast.makeText(this, "Downloading...", Toast.LENGTH_LONG).show();
	}
}
